from sklearn.cluster import DBSCAN
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# 读取Excel文件
df = pd.read_excel('B_FQT.xlsx')

# 提取两列数据
column1 = df['Q1-重力异常值梯度'].values.tolist()
column2 = df['Q2-适配区划分'].values.tolist()
column3 = df['位置'].values.tolist()

# 生成二维数组
array = np.array([column3, column1, column2]).T

print(array)
# 使用DBSCAN算法进行密度聚类
db = DBSCAN(eps=20, min_samples=5)
labels = db.fit_predict(array)

# 绘制聚类结果
colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
for label in set(labels):
    if label == -1:
        # 噪声点使用黑色绘制
        color = 'k'
    else:
        color = colors[label % len(colors)]
    mask = labels == label
    plt.scatter(array[mask, 0], array[mask, 1], c=color, label=label)

# 设置图形属性
plt.title('DBSCAN Clustering')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.legend()

# 显示图形
plt.show()
